        TITL DAI FIRMWARE 0EBC1-0ECF7  V1.1
        ORG   :EBC1
*
*************************
* RUN basicfunction PDL *
*************************
*
* A given paddle channel is enabled. Counter 0 is
* set to FFFF. The counter is read over and over
* until it is counted out.
*
* Exit: BC updated, AFDEHL corrupted.
*
RPDL    MVI   A,:05
        CALL  REXIL     Get paddle select (0-5)
        MOV   D,A       into D
        LDA   POROM     Get POROM
        ANI   :F8       ROM/cass.select only
        ORA   D         OR with paddle select
        ORI   :08       Paddle enable
        CALL  MPT53     Load PORO/POROM
        PUSH  B
        MVI   A,:30
        LXI   B,PITCW   Addr 8253 cmd word
        STAX  B         Select ch.0, mode 0, 2 byte
        LXI   H,:FFFF
        SHLD  DCE0      Load counter ch.0
        LDA   PORI1     Get pdl timer trig impulse
 REM (Useless: A is cleared in
 REM 0EBE3)
PDL10   XCHG            DE = FFFF
        MVI   A,:00
        STAX  B         (FC06)=00: counter 0, latch
 REM operation
        LHLD  DCE0      Get contents counter 0
        CALL  COMP      Compare HL-DE
        JC    PDL10     Again if DE > HL
        CALL  CMPHL     HL = 2-compl. of HL
        LXI   D,:FFCE   ) Substract 49
        DAD   D         )
        JC    PDL20     If result negative
        LXI   H,:0000
PDL20   MOV   A,H
        ORA   A
        JZ    PDL30
        MVI   L,:FF
        NOP 
        NOP 
        NOP 
PDL30   MVI   A,:36
        STAX  B         (FC06)=#36: Chan 0, mode 3
        LDA   POROM     Get PORO/POROM
        ANI   :F0       Disable paddle operation
        CALL  MPT52     Load PORO/POROM
        POP   B
        MOV   A,L       A=0 if result negative,
 REM else FF
        JMP   FR1BY     Move A into MACC
*
*
*
**************************
* RUN basicfunction PEEK *
**************************
*
* Returns the contents of a memory location
* with an address given as INT argument.
*
RPEEK   CALL  REXI2     Get addr in HL
        MOV   A,M       Get its contents
        JMP   FR1BY     Move it into MACC
*
************************
* RUN basicfunction PI *
************************
*
* Returns a value of 3.14159.
*
RPI     LXI   H,FPPI    Addr FPT (PI)
        RST   4         FPT (PI) into MACC
        DATA  :0C
        RET 
*
*********************
* DATA - (not used) *
*********************
*
L0E252  DATA  :02       FPT (PI)
        DATA  :C9
        DATA  :0F
        DATA  :DB
*
*************************
* RUN basicfunction RND *
*************************
*
* Returns a random or pseudo-random number.
* If argument > 0: Returns a pseudo-random number
*                  in the range 0 <= R < argument.
* If argument = 0: Returns a hardware random number
*                  0 < R < 1.
* If argument < 0: Number replaces the kernel for
*                  calculating further pseudo-
*                  random numbers.
*
RRND    CALL  FTEST     Test if arg is 0
        JZ    RRAND     Then hardware random
        LXI   H,RNUM    Addr random number kernel
        JP    :EC35     If pseudo random number
        RST   4         Copy MACC to kernel
        DATA  :0F
        XCHG
        LXI   H,WORKE   Addr scratch area WORKE
        RST   4         Copy MACC to WORKE
        DATA  :0F
        PUSH  H         Saveaddr WORKE
        XCHG
        PUSH  H         Save addr RNUM
        MVI   M,:01     Limit range 1-2
        RST   4         Copy last nr from RNUM
        DATA  :0C       into MACC
        MVI   D,:05
RRD10   LXI   H,RNDA    Addr RNDA
        RST   4         Multiply RO*RNDA
        DATA  :54
        LXI   H,RNDB    Addr RNDB
        RST   4         Add RNDB to RO*RNDA
        DATA  :4E
        NOP 
        NOP 
        NOP 
        NOP 
        NOP 
        LXI   H,IRAND   Addr AND mask
        RST   4         IAND: pick out mantissa
        DATA  :63
        LXI   H,IROR    Addr OR mask
        RST   4         IOR: set mantissa top
        DATA  :66       bit, + range 1-2
        DCR   D
        JNZ   RRD10     Again if D<>0
        POP   H         Get addr RNUM
        RST   4         Copy MACC to RNUM
        DATA  :0F
        LXI   H,FPM1    Addr FPT (-1)
        RST   4         Add -1 to MACC (range
        DATA  :00       0-1)
        POP   H         Get addr WORKE
        RST   4         Frig range: multiply
        DATA  :06       MACC*(WORKE)
        RET 
*
***************************
* part of RUN TALK (CD64) *
***************************
*
* Entry: Z=1: Code = 0C (delay).
*        Z=0: Code = 0D (ML call)
*
MPT46   MOV   E,M       )
        INX   H         ) Wait-time/ML address
        MOV   D,M       ) in HL
        INX   H         )
*
********************
* part of RUN TALK *
********************
*
* A bug is removed, caused by XCHG in #EC71. The
* return from #EC78 (JMP CD67) could not be executed
* correctly, because of a wrong address stored in
* the HL-registers. This problem existed only for
* the 'TALK'-codes #0C (wait) and #0D (ML call).
*
* Entry: DE: Wait time or address ML-routine.
*
        CZ    RTK20     ] If to be waited
        CNZ   :CD62     ] Goto MLP address
        JMP   RTK10     Handle next code
        DATA  :FF
*
*************************
* RUN basicfunction SGN *
*************************
*
* Takes a FPT value and returns:
*               +1  if value is positive.
*                0  if value is zero.
*               -1  if value is negative.
*
RSGN    CALL  FTEST     Test if variable is zero
        RZ              Then ready
        LXI   H,FPM1    Addr FPT(-1)
        RST   4         Copy -1 into MACC
        DATA  :0C
        JM    L0E257    Ready if already negative
        RST   4         Else change sign MACC
        DATA  :1B       (make MACC +1)
L0E257  RET 
*
***********************
* TEST A FPT VARIABLE *
***********************
*
* Entry: Variable in MACC.
* Exit:  Z=1: Variable is zero.
*        Z=0: Other flags set on exponent byte
*             of variable.
*        ABCDEHL preserved.
*
FTEST   PUSH  B
        PUSH  D
        PUSH  PSW
        RST   4         Copy MACC to reg A,B,C,D
        DATA  :15
        MOV   E,A       Exp byte in E
        ORA   B         )
*
***********************
* TEST A FPT VARIABLE *
***********************
*
* The old routine tested all bytes of the FPT nr.
* Now only the exponent byte and the hibyte of the
* mantissa is tested. In this way, very small FPT
* numbers are considered to be zero.
*
        JZ    FTS10     ] Abort if A and B are 0
        MOV   A,E       ] Exp byte in A
        ORI   :01       ] Clear CY-flag
        NOP             ]
FTS10   POP   D
        MOV   A,D
        POP   D
        POP   B
        RET 
*
**************************
* RUN basicfunction SCRN *
**************************
*
RSCRN   CALL  RCOOR     Eval given coord
        PUSH  B
        MOV   C,A       Y-coord in C
        RST   5         Ask colour of dot on screen
        DATA  :27       + size graphics screen
        POP   B
        JC    SCRER     Evt run screen error
        JMP   FR1BY     Contents screen loc in MACC
*
*
*  ==============
*** LIST HANDLER ***
*  ==============
*
* This module lists a program from the textbuffer
* onto the screen (or into other required direction)
*
***********************
* LIST A PROGRAM LINE *
***********************
*
* Entry: BC: Points to start of textline.
* Exit:  BC: Points to start of next line.
*        DEHL preserved, AF corrupted.
*
SLINE   PUSH  D
        PUSH  H
        INX   B         Pnts to line nr
        CALL  L0E318    List line nr
        MVI   A,:08
        CALL  PTAB      Cursor to tab 8
L0E262  CALL  SCOM      List statement
        LDAX  B         Get next byte
        ORA   A
        JP    L0E263    If no more statements
        CALL  SCHRI     Else: print ':'
        DATA  :3A
        JMP   L0E262    List next statement
L0E263  CALL  SCHRI     print car.ret
        DATA  :0D
        POP   H
        POP   D
        RET 
*
********************
* LIST A STATEMENT *
********************
*
* Based on the token in the textbuffer, a particular
* statement will be printed.
* At first, the Basiccommand will be printed. The
* pointers to the particular strings are in a table
* starting at CD8B. The base for the table is CC08;
* the offset is calculated by TOKEN *3.
*
* The databyte after the stringaddress pointer
* indicates which list-routine has to be used for
* the rest of the statement. This byte is a offset
* for table ECF8.
*
* Entry: BC: Points to token.
* Exit:  BC: Points to next statement.
*        AFDEHL corrupted.
*        On stack: Returnaddress from this sub-
*        routine.
*
SCOM    LDAX  B         Get token
        INX   B         Update pointer
        MOV   E,A       token in E
        MVI   D,:00
        LXI   H,:CC08   Startaddr stringtable
        DAD   D         ) Add 3* token
        DAD   D         )
        DAD   D         )
        MOV   E,M       Get lobyte stringaddr
        INX   H
        MOV   D,M       Get hibyte stringaddr
        INX   H         Point to data after addr
        XCHG            Stringaddr in HL
        MOV   A,M       Get length byte of string
        ORA   A         Length=0?
        CALL  PSTR      List Basiccmd string
        LDAX  D         Get data byte after string-
 REM address
        JZ    SCM10     If length string =0
        CPI   :00
        CNZ   SCHSP     Print space if byte after
 REM stringaddr <>0
SCM10   LDAX  D         Get data byte (= offset)
        ADD   A         Offset *2
        MOV   E,A       in E
        MVI   D,:00
        LXI   H,L0E355  Startaddr table Listroutines
        DAD   D         Add offset
        MOV   E,M       Get addr in DE
        INX   H
        MOV   D,M
        XCHG            Addr routine in HL
        PCHL            Go to this adress
        UNL
REXIL   EQU   :E743
POROM   EQU   :0040
MPT53   EQU   :D808
PITCW   EQU   :FC06
DCE0    EQU   :FC00
PORI1   EQU   :FD01
COMP    EQU   :DE14
CMPHL   EQU   :DE26
MPT52   EQU   :D806
FR1BY   EQU   :EB7C
REXI2   EQU   :E6F8
FPPI    EQU   :D0F5
RRAND   EQU   :E40C
RNUM    EQU   :012D
WORKE   EQU   :0129
RNDA    EQU   :C6A8
RNDB    EQU   :C6AC
IRAND   EQU   :D0FD
IROR    EQU   :C6B0
FPM1    EQU   :D0F1
RTK20   EQU   :DACC
RTK10   EQU   :CD67
RCOOR   EQU   :E5F3
SCRER   EQU   :E602
L0E318  EQU   :EFAE
PTAB    EQU   :DB2A
SCHRI   EQU   :EFF5
PSTR    EQU   :DB32
SCHSP   EQU   :CE6B
L0E355  EQU   :ECF8
        LST
        END 
